package com.miguo.ui.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.miguo.app.AppHelper;
import com.miguo.plugins.share.ShareWXUtils;
import com.miguo.view.listener.ShareListener;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

/**
 * 微信客户端回调
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;

    private static ShareListener mListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = ShareWXUtils.createWXAPI(this);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onResume() {
        super.onResume();
        this.finish();
    }

    public static void invoke(Activity activity, ShareListener listener) {
        mListener = listener;
        Intent intent = new Intent(activity, WXEntryActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public void onReq(BaseReq req) {
        //微信回调应用
        AppHelper.StartMain(this);
        /*
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                AppHelper.StartMain(this);
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            default:
                AppHelper.StartMain(this);
                break;
        }
        */
    }

    @Override
    public void onResp(BaseResp resp) {
        if(mListener!=null) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    //分享成功
                    mListener.onComplete();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    //分享取消
                    mListener.onCancel();
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    //分享拒绝
                    mListener.onError();
                    break;
                default:
                    //分享异常
                    mListener.onError();
                    break;
            }
        }
        this.finish();
    }
}
